package rexsee.core.widget;

import rexsee.core.style.RexseeDrawable;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

public class ProgressDiv extends Div {

	private final TextView mTrack;
	private final TextView mProgress;

	public ProgressDiv(Context context) {

		super(context);
		mTrack = new TextView(context);
		mProgress = new TextView(context);
		FrameLayout layout = new FrameLayout(context);
		layout.addView(mTrack);
		layout.addView(mProgress);
		this.addDivChild(layout);

		styleSheet.parseStyle("width:fillparent;height:5px;font-size:12px;border-width:0px;label-padding:2 0 2 10;");

		mTrack.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT));
		mProgress.setLayoutParams(new FrameLayout.LayoutParams(1, FrameLayout.LayoutParams.FILL_PARENT));

		mTrack.setGravity(Gravity.CENTER_VERTICAL);
		mTrack.setFocusable(false);
		mTrack.setClickable(false);
		mTrack.setSingleLine(true);

		mProgress.setGravity(Gravity.CENTER_VERTICAL);
		mProgress.setFocusable(false);
		mProgress.setClickable(false);
		mProgress.setSingleLine(true);

	}

	protected void setProgressStyle() {

		mTrack.setTextSize(Float.parseFloat(styleSheet.font_size.toLowerCase().replaceAll("px", "")));
		mProgress.setTextSize(Float.parseFloat(styleSheet.font_size.toLowerCase().replaceAll("px", "")));
		mTrack.setTypeface(null, styleSheet.getTypefaceStyle());
		mProgress.setTypeface(null, styleSheet.getTypefaceStyle());
		mTrack.setPadding(styleSheet.getLabelPaddingLeft(), styleSheet.getLabelPaddingTop(), styleSheet.getLabelPaddingRight(), styleSheet.getLabelPaddingBottom());
		mProgress.setPadding(styleSheet.getLabelPaddingLeft(), styleSheet.getLabelPaddingTop(), styleSheet.getLabelPaddingRight(), styleSheet.getLabelPaddingBottom());
		mTrack.setShadowLayer(
				Float.parseFloat(styleSheet.text_shadow_radius),
				Float.parseFloat(styleSheet.text_shadow_dx.toLowerCase().replaceAll("px", "")),
				Float.parseFloat(styleSheet.text_shadow_dy.toLowerCase().replaceAll("px", "")),
				Color.parseColor(styleSheet.text_shadow_color));
		mProgress.setShadowLayer(
				Float.parseFloat(styleSheet.text_shadow_radius),
				Float.parseFloat(styleSheet.text_shadow_dx.toLowerCase().replaceAll("px", "")),
				Float.parseFloat(styleSheet.text_shadow_dy.toLowerCase().replaceAll("px", "")),
				Color.parseColor(styleSheet.text_shadow_color));

		mTrack.setTextColor(Color.parseColor(styleSheet.color));
		mTrack.setBackgroundDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.label_background_color));

		mProgress.setTextColor(Color.parseColor(styleSheet.progress_text_color));
		mProgress.setBackgroundDrawable(RexseeDrawable.getDrawable(mContext, null, styleSheet.progress_color));

		int progressInt = Integer.parseInt(styleSheet.progress.replaceAll("%", ""));
		int width = (progressInt >= 100) ? FrameLayout.LayoutParams.FILL_PARENT : mTrack.getWidth() * progressInt / 100;
		mProgress.setLayoutParams(new FrameLayout.LayoutParams(width, FrameLayout.LayoutParams.FILL_PARENT));
		if ((mProgress.getHeight() >= (mProgress.getTextSize() + mProgress.getPaddingTop() + mProgress.getPaddingBottom()))) {
			mTrack.setText(Html.fromHtml(styleSheet.label));
			mProgress.setText(Html.fromHtml(styleSheet.label));
		}

		setDivStyle();

	}
	protected void setProgressStyle(String style) {
		styleSheet.parseStyle(style);
		setProgressStyle();
	}
	@Override
	public void setStyle(String style) {
		setProgressStyle(style);
	}

}
